package com.oamicnet.controller;

import com.oamicnet.entity.Student;
import com.oamicnet.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student2")
public class Student2Controller {

    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    public String saveStudent(@RequestBody Student student) {
        studentService.save(student);
        return "redirect:/student2/list";
    }

    @PostMapping("/update")
    public String updateStudent(@RequestBody Student student) {
        studentService.update(student);
        return "redirect:/student2/list";
    }

    @GetMapping("/delete")
    public String deleteStudentById(@RequestParam("id") Integer id) {
        studentService.delete(id);
        return "redirect:/student2/list";
    }

    @GetMapping("/list")
    @ResponseBody
    public List<Student> getAllStudents(Model model) {
        List<Student> studentList = studentService.findAll();
        return studentList;
    }
}
